/**
 * 初始化商品分类管理详情对话框
 */
var GoodsCategoryInfoDlg = {
    goodsCategoryInfoData: {},
    zTreeInstance: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '分类名称不能为空'
                }
            }
        },
        pName: {
            validators: {
                notEmpty: {
                    message: '上级名称不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
GoodsCategoryInfoDlg.clearData = function () {
    this.goodsCategoryInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsCategoryInfoDlg.set = function (key, val) {
    this.goodsCategoryInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsCategoryInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
GoodsCategoryInfoDlg.close = function () {
    parent.layer.close(window.parent.GoodsCategory.layerIndex);
}

/**
 * 点击商品ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
GoodsCategoryInfoDlg.onClickCate = function (e, treeId, treeNode) {
    $("#pName").attr("value", GoodsCategoryInfoDlg.zTreeInstance.getSelectedVal());
    $("#parentId").attr("value", treeNode.id);
}

/**
 * 显示商品分类的树
 *
 * @returns
 */
GoodsCategoryInfoDlg.showGoodsCateSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentCateMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏商品分类的树
 */
GoodsCategoryInfoDlg.hideGoodsCateSelectTree = function () {
    $("#parentCateMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
GoodsCategoryInfoDlg.collectData = function () {
    this
        .set('id')
        .set('parentId')
        .set('parentIds')
        .set('name')
        .set('sort')
        .set('remarks')
}

/**
 * 验证数据是否为空
 */
GoodsCategoryInfoDlg.validate = function () {
    $('#productCateForm').data("bootstrapValidator").resetForm();
    $('#productCateForm').bootstrapValidator('validate');
    return $("#productCateForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
GoodsCategoryInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goodsCategory/add", function (data) {
        Feng.success("添加成功!");
        window.parent.GoodsCategory.table.refresh();
        GoodsCategoryInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsCategoryInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
GoodsCategoryInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goodsCategory/update", function (data) {
        Feng.success("修改成功!");
        window.parent.GoodsCategory.table.refresh();
        GoodsCategoryInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsCategoryInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCateMenu" || $(
        event.target).parents("#parentCateMenu").length > 0)) {
        GoodsCategoryInfoDlg.hideGoodsCateSelectTree();
    }
}


$(function () {
    Feng.initValidator("productCateForm", GoodsCategoryInfoDlg.validateFields);

    var ztree = new $ZTree("parentCateMenuTree", "/goodsCategory/tree");
    ztree.bindOnClick(GoodsCategoryInfoDlg.onClickCate);
    ztree.init();
    GoodsCategoryInfoDlg.zTreeInstance = ztree;
});
